import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import SignaturePad from "signature_pad";

/**
 * Generated class for the SignaturePadPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-signature-pad',
    templateUrl: 'signature-pad.html',
})
export class SignaturePadPage {

    canvas;
    signaturePad: SignaturePad;

    height = screen.height;
    width = screen.width;

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private viewCtrl: ViewController) {
    }

    ngAfterViewInit() {
        this.init();
    }

    init() {
        this.canvas = document.getElementById("myCanvas");
        this.signaturePad = new SignaturePad(this.canvas);
    }

    save() {
        let data = this.signaturePad.toDataURL();
        let emptyFlag = this.signaturePad.isEmpty();
        this.viewCtrl.dismiss({imageData: data , emptyFlag: emptyFlag});
    }

    clear() {
        this.signaturePad.clear();
    }

    cancel() {
        this.navCtrl.pop();
    }
}
